package top.whysu.rongcloud.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import top.whysu.rongcloud.constant.CodeConstant;
import top.whysu.rongcloud.constant.RongCloudConstant;
import top.whysu.rongcloud.dao.RongCloudGroupMemberDao;
import top.whysu.rongcloud.dao.RongCloudUserDao;
import top.whysu.rongcloud.exception.BusinessException;
import top.whysu.rongcloud.po.RongCloudGroupMemberPO;
import top.whysu.rongcloud.po.RongCloudUserPO;
import top.whysu.rongcloud.service.RongCloudGroupMemberService;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service("RongCloudGroupMemberServiceImpl")
public class RongCloudGroupMemberServiceImpl implements RongCloudGroupMemberService {

    @Resource
    RongCloudGroupMemberDao rongCloudGroupMemberDao;
    @Resource
    RongCloudUserDao rongCloudUserDao;

    @Override
    public synchronized void wholeSaveMembers(BigDecimal groupId, List<String> memberAccountList, String createAccount) {
        if (CollectionUtils.isEmpty(memberAccountList) || groupId == null) {
            throw new BusinessException(CodeConstant.RongCloud.GROUPS_OPERATE_FAIL, "请指定要添加的群成员以及群组！");
        }
        for (String memberAccount : memberAccountList) {
            // 保存群成员
            saveMember(groupId, memberAccount, createAccount);
        }
    }

    /**
     * 保存群成员（加了锁）
     *
     * @param groupId       数组ID
     * @param memberAccount 需要保存的群成员账号集合 （如果群成员已经存在的话，则忽略）
     * @param createAccount 群主账号（允许为空）
     * @return void
     * @author whysu @2021-03-17 10:51
     */
    private synchronized void saveMember(BigDecimal groupId, String memberAccount, String createAccount) {
        RongCloudGroupMemberPO memberPO = new RongCloudGroupMemberPO();
        memberPO.setGroupId(groupId);
        memberPO.setMemberAccount(memberAccount);
        memberPO.setUpdateTime(new Date());
        // 群主，还是普通用户？
        Integer role = null;
        if (StringUtils.isNotBlank(createAccount)) {
            if (createAccount.equals(memberAccount)) {
                role = RongCloudConstant.Groups.Role.OWNER;
            } else {
                role = RongCloudConstant.Groups.Role.NORMAL;
            }
        }
        memberPO.setRole(role);
        // 查询当前账号是否已经在群组中
        RongCloudGroupMemberPO oldPO = rongCloudGroupMemberDao.selectByMemberAccountAndGroupsId(memberAccount, groupId);
        if (oldPO != null) {
            // 编辑
            memberPO.setId(oldPO.getId());
            rongCloudGroupMemberDao.update(memberPO);
        } else {
            // 新增
            memberPO.setCreateTime(new Date());
            // 如果有昵称的话，群备注就是昵称，否则的话就默认账号（编辑不需要改昵称）
            RongCloudUserPO rongCloudUserPO = rongCloudUserDao.selectByAccount(memberAccount);
            if (rongCloudUserPO != null && StringUtils.isNotBlank(rongCloudUserPO.getNickname())) {
                memberPO.setDisplayName(rongCloudUserPO.getNickname());
            } else {
                memberPO.setDisplayName(memberAccount);
            }
            rongCloudGroupMemberDao.insert(memberPO);
        }
    }

    @Override
    public Integer selectRole(String memberAccount, BigDecimal groupId) {
        RongCloudGroupMemberPO memberPO = rongCloudGroupMemberDao.selectByMemberAccountAndGroupsId(memberAccount, groupId);
        if (memberPO != null) {
            return memberPO.getRole();
        }
        return null;
    }


}
